package com.hisan.freeride.home.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import com.hisan.freeride.R;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.listener.IEditTextChangeListener;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.WorksSizeCheckUtil;
import com.hisan.freeride.databinding.NicknameBinding;

/**
 * 创建时间 : 2017/7/3
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：昵称修改页面
 */
public class NickNameActivity extends BaseActivity<NicknameBinding> {
    //内容
    private String content;
    //最大字数
    private int maxWord=8;
    private Intent intent;
    private  String  name="";


    @Override
    protected int getLayoutId() {
        return R.layout.nickname;
    }

    @Override
    protected void initView() {

        Bundle bundle=getIntent().getExtras();
        if (!CollectionUtils.isNullOrEmpty(bundle)){
            name=bundle.getString("name");
            mBinding.titlename.setText(name);
            if (name.equals("修改昵称")){
                mBinding.nametetel.setText("新昵称");
                mBinding.etText.setHint("请输入您的新昵称");
            }else if (name.equals("修改行业")){
                mBinding.nametetel.setText("新行业");
                mBinding.etText.setHint("请输入您的新行业");
            }else if (name.equals("修改公司")){
                mBinding.nametetel.setText("新公司");
                mBinding.etText.setHint("请输入您的新公司");
            }else if (name.equals("修改职业")){
                mBinding.nametetel.setText("新职业");
                mBinding.etText.setHint("请输入您的新职业");
            }
        }
        mBinding.ok.setEnabled(false);
        //1.创建工具类对象 把要改变颜色的Button先传过去
        WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(mBinding.ok);
        //2.把所有要监听的edittext都添加进去
        textChangeListener.addAllEditText(mBinding.etText);
        //3.接口回调 在这里拿到boolean变量 根据isHasContent的值决定 tv 应该设置什么颜色
        WorksSizeCheckUtil.setChangeListener(new IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if(isHasContent){
                    mBinding.ok.setEnabled(true);
                }else{
                    mBinding.ok.setEnabled(false);
                }
            }

            @Override
            public void afterChange(Editable editable) {
                int number = Integer.valueOf(editable.length());
                if (number > maxWord) {
                    if (!CollectionUtils.isNullOrEmpty(content)) {
                        showToast("已到达最大字数！");
                        mBinding.etText.setText(content);
                    }
                } else {
                    content = editable.toString();
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
        mBinding.ok.setOnClickListener(v -> {
            String content1 =mBinding.etText.getText().toString();
            if (CollectionUtils.isNullOrEmpty(content1)){
                showToast("请输入内容");
                return;
            }else{
                if (CollectionUtils.isNullOrEmpty(intent))intent=new Intent();
                intent.putExtra("name", content1);
                intent.putExtra("type",name);
                setResult(97,intent);
                finishActivity(true);
            }
        });
    }

    @Override
    protected void loadData(boolean isRefresh) {

    }
}
